/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "FileSystem.h"
#include "DataSerializer.h"

namespace PQEngine
{
	const std::string FileSystem::EXT_MATERIAL="material";
	const std::string FileSystem::EXT_MESH="mesh";
	const std::string FileSystem::EXT_SKELETON="skeleton";
	const std::string FileSystem::EXT_TGA="tga";
	

	FileSystem::FileSystem()
	{
		/*
		_currentPath=Poco::Path::current();
		std::string homePath=Poco::Path::home();
		std::string temp=Poco::Path::temp();

		*/
	}

	FileSystem& FileSystem::getSingleton()
	{
		static FileSystem inst;
		return inst;
	}

	bool FileSystem::createFile(std::string path)
	{
		/*
		Poco::File file(path);
		if(file.exists()){
			return false;
		}

		bool ret=file.createFile();
		return ret;
		*/
		return false;
	}

	bool FileSystem::isDirExist(std::string path)
	{
		/*
		Poco::File file(path);
		
		if(file.exists()){
			if(file.isDirectory()){
				return true;
			}
		}
		*/
	
		return false;
	}

	bool FileSystem::isFileExist(std::string path)
	{
		/*
		Poco::File file(path);
		
		if(file.exists()){
			if(file.isFile()){
				return true;
			}
		}
		*/
	
		return false;
	}

	bool FileSystem::createDirectory(std::string path)
	{
		/*
		Poco::File file(path);
		if(file.exists()){
			return false;
		}

		file.createDirectories();
		return true;
		*/
		return false;
		
	}

	SerializeDatagram* FileSystem::readGData(std::string fileName)
	{
		return 0;
	}

	void FileSystem::writeGData(std::string fileName,SerializeDatagram* dgram)
	{

	}

	/*
	int test()
	{
		TemporaryFile tmp;
std::ofstream ostr(tmp.path().c_str());
ostr << "Hello, world!" << std::endl;
ostr.close();
return 0;
*/


		//std::set<std::string> files;
//Glob::glob("%WINDIR%\\system32\\*.exe", files);
// Glob::glob("/usr/include/*/*.h", files);

	/*
std::set<std::string>::iterator it = files.begin();
for (; it != files.end(); ++it)
{
std::cout << *it << std::endl;
}
return 0;



		std::string cwd(Path::current());
DirectoryIterator it(cwd);
DirectoryIterator end;
while (it != end)
{
std::cout << it.name();
if (it->isFile())
std::cout << it->getSize();
std::cout << std::endl;
Path p(it.path());
++it;
}
return 0;


		std::string tmpPath(Path::temp());
tmpPath.pushDirectory("PocoFileSample");
File tmpDir(tmpPath);
tmpDir.createDirectories();
bool exists = tmpDir.exists();
bool isFile = tmpDir.isFile();
bool isDir = tmpDir.isDirectory();
bool canRead = tmpDir.canRead();
bool canWrite = tmpDir.canWrite();
File tmpFile(Path(tmpPath, std::string("PocoFileSample.dat")));
if (tmpFile.createFile())
{
tmpFile.setSize(10000);
File tmpFile2(Path(tmpPath, std::string("PocoFileSample2.dat")));
tmpFile.copyTo(tmpFile2.path());
Poco::Timestamp now;
tmpFile.setLastModified(now);
tmpFile.setReadOnly();
canWrite = tmpFile.canWrite();
tmpFile.setWriteable();
canWrite = tmpFile.canWrite();
}
tmpDir.list(files);
std::vector<std::string>::iterator it = files.begin();
for (; it != files.end(); ++it)
{
std::cout << *it << std::endl;
}
tmpDir.remove(true);
return 0;





		std::string shellName("cmd.exe"); // Windows
// std::string shellName("sh"); // Unix
std::string path(Environment::get("PATH"));
Path shellPath;
bool found = Path::find(path, shellName, shellPath);
std::string s(shellPath.toString());
return 0;


		std::string config("%HOMEDRIVE%%HOMEPATH%\\config.ini");
// std::string config("$HOME/config.ini");
std::string expConfig(Path::expand(config));
return 0;


		std::cout
<< "cwd: " << Path::current() << std::endl
<< "home: " << Path::home() << std::endl
<< "temp: " << Path::temp() << std::endl
<< "null: " << Path::null() << std::endl;
return 0;

		Path p("/usr/include/stdio.h", Path::PATH_UNIX);
Path parent(p.parent());
std::string s(parent.toString(Path::PATH_UNIX)); // "/usr/include/"
Path p1("stdlib.h");
Path p2("/opt/Poco/include/Poco.h", Path::PATH_UNIX);
p.resolve(p1);
s = p.toString(Path::PATH_UNIX); // "/usr/include/stdlib.h"
p.resolve(p2);
s = p.toString(Path::PATH_UNIX); // "/opt/Poco/include/Poco.h"
return 0;

		Path p("c:\\projects\\poco\\build_vs80.cmd", Path::PATH_WINDOWS);
std::string device(p.getDevice()); // "c"
int n = p.depth(); // 2
std::string dir1(p.directory(0)); // "projects"
std::string dir2(p[1]); // "poco"
std::string fileName(p[2]); // "build_vs80.cmd"
fileName = p.getFileName();
std::string baseName(p.getBaseName()); // "build_vs80"
std::string extension(p.getExtension()); // "cmd"
p.setBaseName("build_vs71");
fileName = p.getFileName(); // "build_vs71.cmd"
return 0;


		Poco::Path p(true); // path will be absolute
		p.setNode("VMS001");
		p.setDevice("DSK001");
		p.pushDirectory("POCO");
		p.pushDirectory("INCLUDE");
		p.pushDirectory("POCO");
		p.setFileName("POCO.H");
		std::string s(p.toString(Poco::Path::PATH_VMS));
		// "VMS001::DSK001:[POCO.INCLUDE.POCO]POCO.H"
		p.clear(); // start over with a clean state
		p.pushDirectory("projects");
		p.pushDirectory("poco");
		s = p.toString(Poco::Path::PATH_WINDOWS); // "projects\poco\"
		s = p.toString(Poco::Path::PATH_UNIX); // "projects/poco/"
		s = p.toString(); // depends on your platform


		//creating a path will work independent of the OS
		Path p("C:\\Windows\\system32\\cmd.exe");
		Path p("/bin/sh");
		p = "projects\\poco";
		p = "projects/poco";
		p.parse("/usr/include/stdio.h", Path::PATH_UNIX);
		*/

		//bool ok = p.tryParse("/usr/*/stdio.h");
/*
		ok = p.tryParse("/usr/include/stdio.h", Path::PATH_UNIX);
		ok = p.tryParse("/usr/include/stdio.h", Path::PATH_WINDOWS);
		ok = p.tryParse("DSK$PROJ:[POCO]BUILD.COM", Path::PATH_GUESS);
		return 0;
	}
*/

}
